/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker.ui;

import java.awt.Frame;
import java.util.Date;
import ru.example.netcracker.controller.ControllerFacade;
import ru.example.netcracker.model.entity.Alarm;

/**
 *
 * @author Nikolay
 */
public class AlarmDialog extends javax.swing.JDialog {

    public static AlarmDialog newInstance(Frame frame, ControllerFacade ctrl, Alarm alarm) {
        return new AlarmDialog(frame, ctrl, alarm);
    }    

    public static AlarmDialog newInstance(Frame frame, ControllerFacade ctrl) {
        return new AlarmDialog(frame, ctrl);
    }
    
    private final ControllerFacade ctrl;    
    private Alarm alarm;
    
    public AlarmDialog(Frame frame, ControllerFacade ctrl, Alarm alarm) {
        this(frame, ctrl);
        this.alarm = alarm;
        clockPanel1.setDateTime(alarm.getDate());
    }
    
    public AlarmDialog(Frame frame, ControllerFacade ctrl) {
        super(frame, false);
        this.ctrl = ctrl;
        initComponents();
    }
    
    public Alarm getAlarm() {
        return alarm;
    }

    public void setAlarm(Alarm alarm) {
        this.alarm = alarm;
        clockPanel1.setDateTime(alarm.getDate());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        clockPanel1 = new ru.example.netcracker.ui.ClockPanel();
        updateButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        clockPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        clockPanel1.setPreferredSize(null);

        updateButton.setText("Отложить");
        updateButton.setPreferredSize(new java.awt.Dimension(170, 25));
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Завершить");
        cancelButton.setPreferredSize(new java.awt.Dimension(170, 25));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(clockPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(updateButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(clockPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        long time = alarm.getDate().getTime() + 5 * 60 * 1000; // отложить на 5 минут
        ctrl.stopPlayer();
        ctrl.updateAlarm(alarm.getId(), alarm.getName(), alarm.getPath(), new Date(time));
        dispose();
    }//GEN-LAST:event_updateButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        ctrl.stopPlayer();
        ctrl.deleteTask(alarm.getId());
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        ctrl.stopPlayer();
        ctrl.deleteTask(alarm.getId());
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private ru.example.netcracker.ui.ClockPanel clockPanel1;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
}
